package com.dongwu.home;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;
import org.springframework.security.access.annotation.Secured;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baidu.ueditor.ActionEnter;

@Controller
public class HomeContoller {

	
	@RequestMapping("/")
	public String index(String id) {
		return "index";
	}
	
	@Secured("ROLE_ADMIN")
	@RequestMapping("/sessionId")
	@ResponseBody
	public String sessionId(HttpServletRequest request) {
		HttpSession session = request.getSession();
		String value = session.getId();
		return value;
	}
	
//	@RequestMapping("/user/index")
//	public String userIndex() {
//		return "user/index";
//	}

	@RequestMapping("/login")
	public String login() {
		return "login";
	}

	@RequestMapping("/login-error")
	public String loginError(Model model) {
		model.addAttribute("loginError", true);
		return "login";
	}
	
	@RequestMapping("/demo/test")
	public String test() {
		return "demo/message/message-list";
	}
	
    @RequestMapping(value = "/ueditor")  
    public void config(HttpServletRequest request, HttpServletResponse response) {  
        response.setContentType("application/json");  
        String rootPath = request.getSession().getServletContext()  
                .getRealPath("/");  
        try {  
            String exec = new ActionEnter(request, rootPath).exec();  
            PrintWriter writer = response.getWriter();  
            writer.write(exec);  
            writer.flush();  
            writer.close();  
        } catch (IOException | JSONException e) {  
            e.printStackTrace();  
        }  
    }  
	
}
